﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Activation;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Samples.WebHost
{
	public class ServiceFactory : ServiceHostFactory
	{
		protected override ServiceHost CreateServiceHost( Type serviceType, Uri[] baseAddresses )
		{
			var host = base.CreateServiceHost( serviceType, baseAddresses );
			host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
			host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new Samples.WebHost.Validators.UserNameValidator(  );
			var binding = new Albatross.WCF.Extenions.Bindings.ClearUsernameBinding();
			//binding.SetMessageVersion( MessageVersion.Soap12 );

			var compression = new Albatross.WCF.Extenions.ServiceModel.Channels.CompressionBindingElement();
			compression.Algorithm = Albatross.WCF.Extenions.ServiceModel.Channels.CompressionAlgorithm.GZip;
			compression.Level = Albatross.WCF.Extenions.ServiceModel.Channels.CompressionLevel.Normal;

			binding.ClearElement.Add( compression );

			binding.ClearElement.Add( new TextMessageEncodingBindingElement()
			{
				MessageVersion = MessageVersion.Soap12
			} );
			binding.ClearElement.Add( SecurityBindingElement.CreateUserNameOverTransportBindingElement() );
			binding.ClearElement.Add( new Albatross.WCF.Extenions.Bindings.AutoSecuredHttpTransportElement() );
			
			host.AddServiceEndpoint( typeof( Albatross.Common.WCF.IWcfRequestProcessorForDotNetClient ), binding, baseAddresses[ 0 ] );//"http://Home.Sepal.ws:8000/RequestProcessor.svc"
			return host;
		}
		public override ServiceHostBase CreateServiceHost( string constructorString, Uri[] baseAddresses )
		{
			return base.CreateServiceHost( constructorString, baseAddresses );
		}
	}

}
